import java.util.Arrays;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-02-24
 * Time:22:52
 */
public class TestHeap {
    public int elem[];
    public int usedSize;

    public TestHeap(){
        this.elem = new int[10];
    }

    /*
    向下调整
    parent-每棵树的根节点
    len-每棵树的调整的结束位置
     */
    public void shiftDown(int parent,int len){
        int child = 2*parent+1;
        while (child < len){
            if (child+1 < len && elem[child] < elem[child+1]){
                child++;//保证当前左右孩子的最大值的下标
            }
            if (elem[child] > elem[parent]){
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }

    /*
    创建一个大根堆
     */
    public void createHeap(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
        //根据代码 显示的时间复杂度   看起来 应该是O(n*logn)  但是 实际上是O(n)
        for (int parent = (usedSize-1-1)/2; parent >= 0 ; parent--) {
            shiftDown(parent,usedSize);
        }
    }

    /*
    向上调整
     */
    public void shiftUp(int child){
        int parent = (child-1)/2;
        while (child > 0){
            if (elem[child] > elem[parent]){
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                child = parent;
                parent = (child-1)/2;
            }else {
                break;
            }
        }
    }

    /*
    入队操作
     */
    public void offer(int val){
        if (isFull()){
            //二倍扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
        shiftUp(usedSize-1);
    }

    public boolean isFull(){
        return usedSize == elem.length;
    }

    /*
    出队操作
     */
    public int poll(){
        if (isEmpty()){
            throw new RuntimeException("优先级队列为空！");
        }
        int tmp = elem[0];
        elem[0] = elem[usedSize-1];
        elem[usedSize-1] = tmp;
        usedSize--;
        shiftDown(0,usedSize);
        return tmp;
    }

    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("优先级队列为空！");
        }
        return elem[0];
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

    /*
    堆排序
     */
    public void heapSort(){
        int end = usedSize-1;
        while(end>0){
            int tmp = elem[0];
            elem[0] = elem[end];
            elem[end] = tmp;
            shiftDown(0,end);
            end--;
        }
    }

    public static void main(String[] args) {
        int[] stones = {2,7,4,1,8,2};
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a,b)->b-a);//创建最大堆
        for (int i = 0; i < stones.length; i++) {
            priorityQueue.add(stones[i]);
        }
        while(priorityQueue.size()>1){
            int a = priorityQueue.poll();
            int b = priorityQueue.poll();
            if (a>b){
                priorityQueue.add(a-b);
            }
        }
        int ret = priorityQueue.isEmpty() ? 0 : priorityQueue.poll();
    }

}
